package com.resou.reader.bookstore.category;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.SortDataBean;
import com.resou.reader.api.entry.SortSubBean;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.base.v.BaseActivity;
import com.resou.reader.bookstore.category.view.IAllCategoryView;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.ViewHelper;
import com.resou.reader.view.Loading;
import com.resou.reader.view.MagincIndicatorAdapter;
import com.resou.reader.view.PagerSlidingTabStrip;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AllCategoryActivity extends ResouBaseActivity<AllCategoryPresenter> implements IAllCategoryView {

    @BindView(R.id.mMagicIndicator)
    MagicIndicator mMagicIndicator;
    @BindView(R.id.category_pager)
    ViewPager mViewPager;
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    List<String> list = null;
    private List<AllCategoryFragment> fragments = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_all_catagory;
    }

    @Override
    public void retryRefreshData() {
        showLoading();
        mPresenter.quearData();
    }

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        mPresenter = new AllCategoryPresenter(this);
    }

    @Override
    protected void initView() {
        tvTitle.setText("分类");
        list = new ArrayList<>();
        list.add("男生");
        list.add("女生");
    }


    @Override
    public Context getViewContext() {
        return this;
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, AllCategoryActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void refresh(final List<SortDataBean<List<SortSubBean>>> data) {
        dissLoading();
        if (data == null || data.size() <= 0)
            return;
        for (int i = 0; i < Math.min(data.size(), 2); i++) {
            AllCategoryFragment fragment = new AllCategoryFragment();
            fragment.setIndex(i);
            fragment.setData(data.get(i).getSubSort());
            fragment.setOnItemClickListener(onItemClickListener);
            fragments.add(fragment);
        }
        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return data.get(position).getFreetypeName();
            }
        };
        fragmentPagerAdapter.getPageTitle(0);
        mViewPager.setAdapter(fragmentPagerAdapter);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new MagincIndicatorAdapter(list, mViewPager));
        mMagicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
    }

    @Override
    public void showError() {

    }


    @OnClick(R.id.ivBack)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void initData() {
        retryRefreshData();
    }

    @Override
    public void showContent() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void showError(Throwable throwable) {
        showNetErrorView(R.id.rlContent);
        ToastUtil.makeShortToast("网络异常");
        dissLoading();
    }

    @Override
    public void showEmpty() {

    }

    private OnClickedItem onItemClickListener = new OnClickedItem() {

        @Override
        public void onItemClick(int index, int position) {
            mPresenter.startToSubCategory(index, position);
        }
    };

    public interface OnClickedItem {
        void onItemClick(int index, int position);
    }
}
